﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace eTech_Physics_Sandbox
{
    /*
     * This class holds the data used to draw line segements contained by a DrawableLine
     */
    class LineSegment
    {
        Vector2[] _endpoints = new Vector2[2]; //Endpoints of the line segment
        Vector2[] _origin = new Vector2[2]; //Original endpoints of the line segment
        Vector2 _position; //Point between both endpoints
        float _xdist, _ydist, _length, _rotation;

        public Vector2[] EndPoints
        {
            get { return _endpoints; }
            set { _endpoints = value; }
        }

        public Vector2[] Origin
        {
            get { return _origin; } //Prevents setting of origin
        }

        public float XDistance
        {
            get { return _xdist; } //Prevents setting of xdist
        }

        public float YDistance
        {
            get { return _ydist; } //Prevents setting of ydist
        }

        public float Length
        {
            get { return _length; } //Prevents setting of length
        }

        public LineSegment(Vector2[] endpoints)
        {
            _endpoints = endpoints;
            _origin = endpoints;
            setDistances();
            setRotation();
            setPosition();
        }

        private void setDistances() //Does the pythagorean therom
        {
            Vector2 xydist = _endpoints[1] - _endpoints[0];
            _xdist = xydist.X;
            _ydist = xydist.Y;
            _length = (float)Math.Sqrt((_xdist * _xdist) + (_ydist * _ydist));
        }

        private void setRotation()
        {
            float degrees = (float)Math.Atan2(_ydist, _xdist);
            _rotation = degrees;
        }

        private void setPosition()
        {
            _position = _endpoints[0];
        }

        public void Update(GameTime gameTime)
        {
            //TODO: Do physics related update things -NTR
            setRotation();
            setPosition();

        }

        public void Draw(SpriteBatch spriteBatch, Texture2D texture, Vector2 offset)
        {
            //TODO: Draw texture along line segment -NTR
            spriteBatch.Draw(texture, _position - offset, null, Color.White, _rotation, new Vector2(0f, texture.Height/2f), new Vector2(_length/20f, 1), SpriteEffects.None, 0);
        }
    }


}
